package com.ruoyi.yljf.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.SysDictDataVO;
import com.ruoyi.yljf.domain.Pharmacy;
import com.ruoyi.yljf.domain.User;
import com.ruoyi.yljf.domain.dto.ArticleDTO;
import com.ruoyi.yljf.domain.dto.CreateDTO.ArticleCreateDTO;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.domain.vo.ArticleTypeVO;
import com.ruoyi.yljf.domain.vo.ArticleVO;
import com.ruoyi.yljf.domain.vo.SearchVO;
import com.ruoyi.yljf.mapper.PharmacyMapper;
import com.ruoyi.yljf.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.ArticleMapper;
import com.ruoyi.yljf.domain.Article;
import com.ruoyi.yljf.service.IArticleService;

import javax.annotation.Resource;

/**
 * 咨询文章Service业务层处理
 * 
 * @author metaee
 * @date 2025-02-13
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

	@Resource
    private ArticleMapper articleMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private PharmacyMapper pharmacyMapper;

    /**
     * 查询咨询文章列表
     * 
     * @param article 咨询文章
     * @return 咨询文章
     */
    @Override
    public List<Article> selectArticleList(Article article) {
    	LambdaQueryWrapper<Article> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(article.getTitle())) {
			queryWrapper.like(Article::getTitle, article.getTitle());
		}
        if (StringUtils.isNotEmpty(article.getContent())) {
			queryWrapper.eq(Article::getContent, article.getContent());
		}
        if (StringUtils.isNotEmpty(article.getImg())) {
			queryWrapper.eq(Article::getImg, article.getImg());
		}
        if (StringUtils.isNotEmpty(article.getCherryPick())) {
			queryWrapper.eq(Article::getCherryPick, article.getCherryPick());
		}
        if (StringUtils.isNotEmpty(article.getType())) {
			queryWrapper.eq(Article::getType, article.getType());
		}
        if (StringUtils.isNotEmpty(article.getCreateType())) {
			queryWrapper.eq(Article::getCreateType, article.getCreateType());
		}
        if (StringUtils.isNotNull(article.getCreateId())) {
			queryWrapper.eq(Article::getCreateId, article.getCreateId());
		}
        return articleMapper.selectList(queryWrapper);
    }

	@Override
	public List<ArticleVO> selectArticleVOList(Article article) {
		LambdaQueryWrapper<Article> queryWrapper = Wrappers.lambdaQuery();
		if (StringUtils.isNotEmpty(article.getTitle())) {
			queryWrapper.eq(Article::getTitle, article.getTitle());
		}
		if (StringUtils.isNotEmpty(article.getContent())) {
			queryWrapper.eq(Article::getContent, article.getContent());
		}
		if (StringUtils.isNotEmpty(article.getImg())) {
			queryWrapper.eq(Article::getImg, article.getImg());
		}
		if (StringUtils.isNotEmpty(article.getCherryPick())) {
			queryWrapper.eq(Article::getCherryPick, article.getCherryPick());
		}
		if (StringUtils.isNotEmpty(article.getType())) {
			queryWrapper.eq(Article::getType, article.getType());
		}
		if (StringUtils.isNotEmpty(article.getCreateType())) {
			queryWrapper.eq(Article::getCreateType, article.getCreateType());
		}
		if (StringUtils.isNotNull(article.getCreateId())) {
			queryWrapper.eq(Article::getCreateId, article.getCreateId());
		}
		List<Article> articles = articleMapper.selectList(queryWrapper);
		return BeanUtil.copyToList(articles, ArticleVO.class);
	}

	@Override
	public List<ArticleVO> typeConversion(List<Article> articles) {
		List<ArticleVO> collect = articles.stream().map(this::beanProcess).collect(Collectors.toList());
		collect.forEach(articleVO -> {
			collect.forEach(this::incognito);
		});
		return collect;
	}

	@Override
	public List<ArticleTypeVO> queryArticle(List<SysDictDataVO> secondaryType) {

		List<ArticleTypeVO> collect = secondaryType.stream().map(item -> {

			List<Article> articles = this.articleMapper.selectList(
					new LambdaQueryWrapper<>(Article.class)
							.eq(Article::getType, item.getValue())
							.orderByDesc(Article::getId).last("limit 4")
			);
			List<ArticleVO> articleVOS = this.typeConversion(articles);
			return new ArticleTypeVO(item, articleVOS);
		}).collect(Collectors.toList());

		List<Article> articleList = this.articleMapper.selectList(new LambdaQueryWrapper<>(Article.class)
				.eq(Article::getCherryPick, "Y")
				.orderByDesc(Article::getId).last("limit 4"));
		List<ArticleVO> articleVOS = this.typeConversion(articleList);
		ArticleTypeVO articleTypeVO = new ArticleTypeVO("文章精选", "Y", articleVOS);
		collect.add(articleTypeVO);
		return collect;

	}

	@Override
	public void pointLike(Long articleId) {
		articleMapper.pointLike(articleId);
	}

	@Override
	public void forwarding(Long articleId) {
		articleMapper.forwarding(articleId);
	}


	@Override
	public void insertArticle(ArticleCreateDTO dto) {

	}

	@Override
	public ArticleVO queryById(Long id) {
		Article article = this.getById(id);
		ArticleVO articleVO = this.beanProcess(article);
		if (("user").equals(article.getType())) {	//用户创建
			User user = userMapper.selectById(article.getCreateId());
			if (ObjectUtils.isNotNull(user)) {
				articleVO.setUserName(user.getUserName());
				articleVO.setImg(user.getAvatar());
			}
		} else if ("pharmacy".equals(article.getType())) {
			Pharmacy pharmacy = pharmacyMapper.selectById(article.getCreateId());
			if (ObjectUtils.isNotNull(pharmacy)) {
				articleVO.setUserName(pharmacy.getPharmacyName());
				articleVO.setImg(pharmacy.getPharmacyImg());
			}
		}else {
			articleVO.setUserName("管理员");
			// TODO 设置默认头像
			articleVO.setUserImg("");
		}
		return this.beanProcess(article);

	}

	private ArticleVO beanProcess(Article item) {
    	ArticleVO articleVO = new ArticleVO();
		BeanUtils.copyProperties(item,articleVO);
		articleVO.setType(yljfEnums.descOfCode(item.getType()));
		articleVO.setCreateType(yljfEnums.descOfCode(item.getCreateType()));
		incognito(articleVO);
		return articleVO;
	}
	private void incognito(ArticleVO articleVO){
		if(articleVO.getIncognito() == 1 ){
			articleVO.setUserName("匿名用户");
			//TODO 匿名头像需要配置默认头像
			articleVO.setUserImg("匿名用户");
		}else{
			User user = userMapper.selectById(articleVO.getCreateId());
			articleVO.setUserName(user.getUserName());
			articleVO.setUserImg(user.getAvatar());
		}
	}
}
